using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Utility2DLibrary;
using System.Xml;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;

namespace ShipTest2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        ShipPartPrimitive tri;
        ShipPartPrimitive tri2;
        ShipPartPrimitive rect;

        Camera2D camera;

        Matrix worldTranslationMatrix;
        Matrix projectionMatrix;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //// TODO: Add your initialization logic here
            //viewMatrix = Matrix.CreateLookAt(
            //    new Vector3(0.0f, 0.0f, 0.0f),
            //    new Vector3(0.0f, 0.0f, 0.0f),
            //    Vector3.Up);

            //projectionMatrix = Matrix.CreateOrthographicOffCenter(0,
            //    (float)GraphicsDevice.Viewport.Width, 0,
            //    (float)GraphicsDevice.Viewport.Height,
            //    0.0f, 1.0f);

            ////worldMatrix = Matrix.CreateTranslation(-400, -300, 0);
            //worldMatrix = Matrix.Identity;

            // TODO: Add your initialization logic here
            //viewMatrix = Matrix.CreateLookAt(
            //    new Vector3(0.0f, 0.0f, 0.0f),
            //    new Vector3(0.0f, 0.0f, 0.0f),
            //    Vector3.Up);

            projectionMatrix = Matrix.CreateOrthographicOffCenter(0,
                (float)GraphicsDevice.Viewport.Width, 
                (float)GraphicsDevice.Viewport.Height,0,
                0.0f, 1.0f);

            //worldMatrix = Matrix.CreateTranslation(400, 300, 0);
            //worldMatrix = Matrix.CreateTranslation(400, 300, 0);
            worldTranslationMatrix = Matrix.CreateTranslation(400, 300, 0);

            this.IsMouseVisible = true;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            
            tri = Content.Load<ShipPartPrimitive>("Primitives\\RightTriangle");
            tri2 = Content.Load<ShipPartPrimitive>("Primitives\\EquilateralTriangle");
            rect = Content.Load<ShipPartPrimitive>("Primitives\\Rectangle");

            //tri = new ShipPartPrimitive();
            //tri.VertexFile = "vertex file";
            //tri.TextureFile = "tex file";
            //tri.ShaderEffectFile = "shader file";
            //tri.PositionVector = new Vector2(332, 234);
            //tri.Scale = new Vector2(100, 100);

            //Debug(tri);

            camera = new Camera2D(new Vector2(800, 600), 400, 300, 800, 600);
        }
        private void Debug(VisualObject testData)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            using (XmlWriter writer = XmlWriter.Create("test.xml", settings))
            {
                IntermediateSerializer.Serialize(writer, testData, null);
            }
        }
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        float angle = MathHelper.ToRadians(1f);
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                tri.MouseOver = 1;
                //rect.MouseOver = 0;
            }
            else
            {
                tri.MouseOver = 0;
                //rect.MouseOver = 1;
            }

            // TODO: Add your update logic here
            //camera.Zoom = new Vector2(camera.Zoom.X + 0.01f, camera.Zoom.Y + 0.01f);
            
            tri.Orientation  += 1*angle % (float)(2 * Math.PI);
            tri2.Orientation += 2*angle % (float)(2 * Math.PI);
            rect.Orientation += 3*angle % (float)(2 * Math.PI);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //Matrix worldViewProjection = worldMatrix * camera.ViewTransformationMatrix() * projectionMatrix;
            // TODO: Add your drawing code here
            //spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Immediate, SaveStateMode.None, worldViewProjection);
            {
                //Vector2 mousePos = new Vector2(Mouse.GetState().X-800, Mouse.GetState().Y-600);
                //Vector4 mouseTrans = Vector4.Transform(mousePos, Matrix.CreateTranslation(worldTranslationVector));
                //tri.PositionVector = new Vector2(mouseTrans.X, mouseTrans.Y);

                Matrix viewProjection = camera.ViewTransformationMatrix() * projectionMatrix;
                tri.Draw(spriteBatch, worldTranslationMatrix, viewProjection);

                tri2.Draw(spriteBatch, worldTranslationMatrix, viewProjection);
                rect.Draw(spriteBatch, worldTranslationMatrix, viewProjection);

            }
            //spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
